/**
 * Copyright 2009-2021 the original author or authors.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package zy.blue7.mybatis.test.xml;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import zy.blue7.mybatis.test.xml.dao.PersonDao;
import zy.blue7.mybatis.test.xml.model.Person;
import zy.blue7.mybatis.test.xml.util.SqlSessionFactoryUtil;

/**
 * @author blue7
 * @create 2021/5/17 13:53
 */
public class Main {


    public static void main(String[] args) {
//        insert();
//        selectMyPage();
        select();
    }

    public static void insert() {
        SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtil.getSqlSessionFactoryFromJava();
        SqlSession sqlSession = sqlSessionFactory.openSession();
        PersonDao personDao = sqlSession.getMapper(PersonDao.class);
        Person p = new Person();
        p.setAddress("广东省");
        p.setAge(12);
        p.setEmail("157538651@qq.com");
        p.setName("chen");
        p.setPhone("15345634565");
//        personDao.insert(p);
        personDao.insertBySelectKey(p);
        System.out.println(p.toString());
        sqlSession.commit();
        sqlSession.close();
    }

    /**
     * 使用自定义插件，了解插件原理
     */
    public static void selectMyPage() {
        SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtil.getSqlSessionFactory();
        SqlSession sqlSession = sqlSessionFactory.openSession();
        PersonDao personDao = sqlSession.getMapper(PersonDao.class);
        Person p = new Person();
        p.setAddress("广东省");
        p.setAge(12);
        p.setEmail("157538651@qq.com");
        p.setName("chen");
        p.setPhone("15345634565");
        personDao.insert(p);
        System.out.println(p.toString());
        sqlSession.commit();
        sqlSession.close();
    }

    public static void select() {
        SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtil.getSqlSessionFactoryFromJava();
        /**
         * 这里是创建一个默认的 SqlSession，注意 todo 在这一步已经将 拦截器包装到 SqlSession 中的执行器中
         */
        SqlSession sqlSession = sqlSessionFactory.openSession();
        /**
         * 如果 不使用mapper 接口， 则是调用 类似于 select， insert， update，这样的方法
         *   todo 这个 statement 是 mapper.xml文件中的 namespace.id   空间名称 加上 每个select，update，insert，delete 标签的 id属性
         *   <T > T selectOne(String statement);
         *
         *   todo Object parameter 这个是参数，可以是  org.apache.ibatis.session.defaults.DefaultSqlSession#wrapCollection(java.lang.Object)
         *   todo 对象，集合，map。 array，list等等
         *   private Object wrapCollection(final Object object) {
         *     if (object instanceof Collection) {
         *       StrictMap<Object> map = new StrictMap<Object>();
         *       map.put("collection", object);
         *       if (object instanceof List) {
         *         map.put("list", object);
         *       }
         *       return map;
         *     } else if (object != null && object.getClass().isArray()) {
         *       StrictMap<Object> map = new StrictMap<Object>();
         *       map.put("array", object);
         *       return map;
         *     }
         *     return object;
         *   }
         *
         *   <T > T selectOne(String statement, Object parameter);
         *
         *   <E > List < E > selectList(String statement);
         *   <E > List < E > selectList(String statement, Object parameter);
         *
         *    todo  RowBounds rowBounds 这个是分页查询 该类中记录了 limit 相关东西   org.apache.ibatis.session.RowBounds
         *   <E > List < E > selectList(String statement, Object parameter, RowBounds rowBounds);
         *
         *   <K, V > Map < K, V > selectMap(String statement, String mapKey);
         *
         *   <K, V > Map < K, V > selectMap(String statement, Object parameter, String mapKey);
         *
         *   <K, V > Map < K, V > selectMap(String statement, Object parameter, String mapKey, RowBounds rowBounds);
         *
         *   <T > Cursor < T > selectCursor(String statement);
         *
         *   <T > Cursor < T > selectCursor(String statement, Object parameter);
         *
         *   <T > Cursor < T > selectCursor(String statement, Object parameter, RowBounds rowBounds);
         *
         *  void select (String statement, Object parameter, ResultHandler handler);
         *
         *  void select (String statement, ResultHandler handler);
         *
         *  void select (String statement, Object parameter, RowBounds rowBounds, ResultHandler handler);
         *
         *  int insert (String statement);
         *
         *  int insert (String statement, Object parameter);
         *  int update (String statement);
         *
         *  int update (String statement, Object parameter);
         *
         *  int delete (String statement);
         *
         *  int delete (String statement, Object parameter);
         *
         *  void commit ();
         *
         *  void commit ( boolean force);
         *  void rollback ();
         *
         *  void rollback ( boolean force);
         *
         *  List<BatchResult> flushStatements ();
         *
         *  @Override
         *  void close ();
         *
         *  void clearCache ();
         *
         *  Configuration getConfiguration ();
         *
         *  todo 获取mapper 接口 ，通过mapper 接口来实现调用，直接调用该接口中的方法即可实现查询
         *  <T > T getMapper(Class < T > type);
         *
         *  todo 当你查询数据库时，也可以使用原生的 connection 来进行查询
         *  Connection getConnection ();
         */
//        sqlSession.select("", null, null);
//        sqlSession.select("", null, null, null);

        /**
         * todo 目前都使用mapper 接口来实现 增删改查
         */
        PersonDao personDao = sqlSession.getMapper(PersonDao.class);

        Person select = personDao.select(1);
        System.out.println(select);

        Person selectById = personDao.selectById(2);
        System.out.println(selectById);
        sqlSession.commit();
        sqlSession.close();
    }


}
